﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using System.Reflection;
using SimpleRest.Core.Routing;
using SimpleRest.Core.Routing.Handlers;

namespace SimpleRest.Core.Routing
{
    /// <summary>
    /// Service route that when registered will register the service specified as well as all associated operations.
    /// </summary>
    public class ServiceRoute
    {
        /// <summary>
        /// Service type associated to route.
        /// </summary>
        public readonly Type ServiceType;
        /// <summary>
        /// Configuration associated to route.
        /// </summary>
        public readonly Configuration Configuration;
        /// <summary>
        /// Route uri associated to service route.
        /// </summary>
        public readonly string RouteUri;

        public ServiceRoute(Type serviceType, Configuration configuration)
        {
            if (serviceType == null)
                throw new Exception("Unable to register service for null value.");

            this.RouteUri = RouteUtility.GetRoutePrefix(serviceType);

            this.ServiceType = serviceType;
            this.Configuration = configuration;
        }

        internal Route _route;
        internal Route Route 
        {
            get
            {
                if (_route == null)
                {
                    string routePrefix = RouteUtility.GetRoutePrefix(this.ServiceType);
                    _route = new Route(routePrefix, new RouteHandler(routePrefix));
                }

                return _route;
            }
        }

        
    }

    public class ServiceRoute<T> : ServiceRoute
    {
        public ServiceRoute(Configuration configuration)
            : base(typeof(T), configuration) { }
    }
}
